package game.ai.scene;

import game.ai.aoi.AoiAlgorithm;
import game.ai.aoi.AoiSpace;
import game.common.SafeTimer;
import game.common.Updatable;

import java.util.Collection;

public class GameMap implements IScene, Updatable {

	private AoiSpace aoiSpace;

	private static final byte[] heedSceneObjects = { SceneObject.SceneObjType_Hero, SceneObject.SceneObjType_DropedGood, SceneObject.SceneObjType_Horse,
			SceneObject.SceneObjType_MonsterScene };

	private SceneObjManager sceneObjManager = new SceneObjManager(heedSceneObjects);

	protected SafeTimer aoiUpdatTimer = new SafeTimer(500);

	public GameMap() {
		this.aoiSpace = new AoiSpace(this);
	}

	public AoiSpace getAoiSpace() {
		return aoiSpace;
	}

	@Override
	public Collection<SceneObject> getAllObjs() {
		return sceneObjManager.getAllSceneObjectCollection();
	}

	@Override
	public <T> Collection<T> getObjsByType(byte objType) {
		return sceneObjManager.getObjsCollection(objType);
	}

	@Override
	public SceneObject getObjById(int id) {
		return sceneObjManager.getObjById(id);
	}

	@Override
	public <T> Collection<T> getSceneObjByType(byte type) {
		return sceneObjManager.getObjsCollection(type);
	}

	@Override
	public void enterScene(SceneHero player) {
		sceneObjManager.addObj(player);
	}

	@Override
	public void enterScene(SceneMonster scenemonster) {
		sceneObjManager.addObj(scenemonster);
	}

	@Override
	public void leaveScene(SceneHero player, IScene newScene) {
		sceneObjManager.removeVisibleObj(player);
	}

	@Override
	public void leaveScene(SceneMonster sceneMonster) {
		sceneObjManager.removeVisibleObj(sceneMonster);
	}

	@Override
	public void update(long now) {
		if (aoiUpdatTimer.isIntervalOK(now)) {
			AoiAlgorithm.getInstance().update(aoiSpace);
		}
	}

}
